package com.lifeng.fastssmr.web.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.lifeng.fastssmr.pojo.Item;
import com.lifeng.fastssmr.service.ItemService;
import com.lifeng.fastssmr.utils.IdGenerater;
import com.lifeng.fastssmr.utils.ListAndArrayUtils;
import com.lifeng.fastssmr.web.controller.base.BaseController;

@Controller
@Scope("prototype")
@RequestMapping("/item")
public class ItemController extends BaseController{
	private Logger logger = Logger.getLogger(ItemController.class);
	@Autowired
	private ItemService itemService;
	
	@RequestMapping(value="/addUI")
	public String addUI(){
		return "/item/add.jsp";
	}
	
	@RequestMapping(value="/add")
	public String add(Item item,@RequestParam(required=false)MultipartFile[] picFile) throws IOException, Exception{
		if(item.getId()==null){
			item.setId(IdGenerater.getUUID());
		}
		/*List<InputStream> ins = new ArrayList<InputStream>();
		//上传文件的处理
		for(MultipartFile pic:picFile){
			String contentType = pic.getContentType();//文件类型
			String name = pic.getName();//
			String fileName = pic.getOriginalFilename();//原始文件名
			long fileSize = pic.getSize();
			logger.info("contentType:"+contentType+",name:"+name+",fileName:"+fileName+",fileSize:"+fileSize);
			try {
				InputStream inStream = pic.getInputStream();
				ins.add(inStream);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}*/
		itemService.addAndSaveFile(item, picFile);
		return "redirect:list";
	}
	
	@RequestMapping(value="/list")
	public String list(){
		List<Item> items = itemService.findAll();
		model.addAttribute("datas",items);
		return "/item/list.jsp";
	}
	@RequestMapping(value="/get")
	public String getById(String id){
	    Item item = itemService.getById(id);
		model.addAttribute("data",item);
		return "redirect:list";
	}
	
	@RequestMapping(value="/delete/{id}",method=RequestMethod.GET)
	public String delete(@PathVariable("id") String id){
		Item item = itemService.getById(id);
		if(item!=null){
			itemService.delete(item);
		}
		return "redirect:list";
	}
	
	/**
	 * 编辑页面
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/editUI/{id}",method=RequestMethod.GET)
	public String editUI(@PathVariable("id") String id){
		Item item = itemService.getById(id);
		if(item!=null){
			model.addAttribute("data",item);
			//展示图片
			if(!StringUtils.isEmpty(item.getImage())){
				model.addAttribute("imgs",item.getImage().split("~"));
			}
		}
		return "/item/edit.jsp";
	}
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/edit")
	public String edit(Item itemVo,@RequestParam(required=false)MultipartFile[] picFile) throws Exception{
		if(StringUtils.isEmpty(itemVo.getId())){
			throw new Exception("商品的id为空");
		}
		Item item = itemService.getById(itemVo.getId());
		itemVo.setImage(item.getImage());
		BeanUtils.copyProperties(item, itemVo);
		itemService.addAndSaveFile(item, picFile);
		return "redirect:list";
	}
	/**
	 * 获取图片
	 * @param id
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/getImg/{id}",method=RequestMethod.GET)
	public void getImg(@PathVariable("id") String fileId) throws IOException{
		OutputStream out = response.getOutputStream();
		itemService.getImg(out, fileId);
		out.flush();
		out.close();
	}
	/**
	 * 根据图片id删除
	 * @param fileId
	 * @throws IOException
	 */
	@RequestMapping(value="/deleteImg")
	@ResponseBody
	public String deleteImg(String fileId,String itemId) throws IOException{
		JSONObject json = new JSONObject();
		try {
			itemService.deleteFile(fileId);
			Item item = itemService.getById(itemId);
			String images = item.getImage();
			if(!StringUtils.isEmpty(images)){
				String[] image = images.split("~");
				image = ListAndArrayUtils.arrayRemoveOne(image, fileId);
				item.setImage(ListAndArrayUtils.array2String(image, "~"));
				itemService.add(item);
				json.put("msg", "true");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("msg", "false");
		}
		return json.toJSONString();
	}
}
